package com.mb.app.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.dao.TbIndexClassCfgMapper;
import com.mb.app.domain.TbIndexClassCfg;
import com.mb.app.service.ITbIndexClassCfgService;
import com.mb.common.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页类别配置  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-04-03
 */
@Service
@Slf4j
public class TbIndexClassCfgServiceImpl extends ServiceImpl<TbIndexClassCfgMapper, TbIndexClassCfg> implements ITbIndexClassCfgService {

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public List<TbIndexClassCfg> queryList(Map<String, Object> map) {
        List<TbIndexClassCfg> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public int save(TbIndexClassCfg TbIndexClassCfg) {
        TbIndexClassCfg.setCreateTime(new Date());
        return this.baseMapper.insert(TbIndexClassCfg);
    }

    @Override
    public int update(TbIndexClassCfg TbIndexClassCfg) {
        int istre = 0;
        try {
            if (StringUtils.isNotEmpty(TbIndexClassCfg.getPicUrl())) {
                String PicUrl = TbIndexClassCfg.getPicUrl();
                PicUrl = PicUrl.replaceAll(commonConfig.getImgDomain(), "");
                TbIndexClassCfg.setPicUrl(PicUrl);
            }
            if (StringUtils.isNotEmpty(TbIndexClassCfg.getLogoUrl())) {
                String logoUrl = TbIndexClassCfg.getLogoUrl();
                logoUrl = logoUrl.replaceAll(commonConfig.getImgDomain(), "");
                TbIndexClassCfg.setLogoUrl(logoUrl);
            }
            TbIndexClassCfg.setUpdateTime(new Date());
            istre = this.baseMapper.updateById(TbIndexClassCfg);
        } catch (Exception e) {
            log.error("update Exception", e);
        }
        return istre;
    }


    @Override
    public int remove(Long shipId,Long delUserId) {
//        return this.baseMapper.deleteById(appId);
        TbIndexClassCfg TbIndexClassCfg = new TbIndexClassCfg();
        TbIndexClassCfg.setIndexClassId(shipId);
        TbIndexClassCfg.setDeleteTime(new Date());
        TbIndexClassCfg.setDataStatus(0);
        TbIndexClassCfg.setDeleteUserId(delUserId);
        return this.baseMapper.updateById(TbIndexClassCfg);
    }

    @Override
    public TbIndexClassCfg getDtl(Long tagId) {
        TbIndexClassCfg TbIndexClassCfg = this.baseMapper.selectByIds(tagId);
        if (TbIndexClassCfg != null && StringUtils.isNotEmpty(TbIndexClassCfg.getPicUrl()) && StringUtils.isNotEmpty(TbIndexClassCfg.getLogoUrl())) {
            TbIndexClassCfg.setPicUrl(commonConfig.getImgDomain() + TbIndexClassCfg.getPicUrl());
            TbIndexClassCfg.setLogoUrl(commonConfig.getImgDomain() + TbIndexClassCfg.getLogoUrl());
        }
        return TbIndexClassCfg;
    }

    @Override
    public boolean indexClassCfgNameExit(Map<String, Object> params) {
        boolean exit;
        exit = this.baseMapper.selectByIndexClassCfgInfo(params).size() > 0;
        return exit;
    }

    @Override
    public TbIndexClassCfg getIndexClassById(Map<String, Object> params) {
        return this.baseMapper.getIndexClassById(params);
    }
}
